﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Furion.Schedule;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlSugar;

namespace Abc.Job;
/// <summary>
/// Timer计时器
/// </summary>
[JobDetail("job_TimerJob", "Timer计时器")]
//文档：26.1.5.6 作业触发器特性及自定义
[PeriodSeconds(1, TriggerId = "trigger_TimerJob")]
public class TimerJob : IJob, IDisposable
{
    private readonly ILogger<TimerJob> _logger;
    private readonly IConfiguration _configuration;
    private readonly IServiceScopeFactory _scopeFactory;//瞬时 或 范围 服务
    private readonly ISqlSugarClient _db; //

    public TimerJob(ILogger<TimerJob> logger
        , IConfiguration configuration
        , IServiceScopeFactory scopeFactory
        , ISqlSugarClient db)
    {
        _logger = logger;
        _configuration = configuration;
        _scopeFactory = scopeFactory;
        _db = db;
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        //using var serviceScope = _scopeFactory.CreateScope();
        //var _cacheService = serviceScope.ServiceProvider.GetService<ISysCacheService>();
        //var repository = _serviceScope.ServiceProvider.GetService<IRepository<SysDataConfig>>();
        //var user = await repository.(1);

        _logger.Information($"{context} {_configuration["key"]}");
        await Task.CompletedTask;
    }

    public void Dispose()
    {
        //_serviceScope?.Dispose();
    }
}
